/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.serc.pensieve.uicontroller;

import org.serc.pensieve.model.CheckList;
import org.serc.pensieve.model.Inspection;
import org.serc.pensieve.model.UseCases;
import org.serc.pensieve.model.Preparation;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import net.sf.json.JSONObject;

/**
 *
 * @author Shani
 */
 
public class PreparationController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession  ss = request.getSession();
//JSONObject S = new JSONObject();

         String tempUname = (String)ss.getAttribute("userName");

         String tempUpass = (String)ss.getAttribute("userPassword");

         String tempUrole = (String)ss.getAttribute("userRole");

         int tempId = -1;

         String tempUid = (String) ss.getAttribute("userID");
         if(tempUid != null){
            tempId = Integer.parseInt(tempUid);
         }
        //Creating object of user role and passing session value to it.
        UserRole inspectorObj = new Preparation(tempUname, tempUpass, tempUrole);
        //Getting Action from Adm()in pages(index.jsp).
        String inspectorAct = request.getParameter("action");
        PrintWriter out = response.getWriter();


        if(inspectorAct.equals("savestate"))
        {


            String inspectionIds = request.getParameter("inspectionId");
            int inspectionId = Integer.parseInt(inspectionIds);

             String usecaseIDs = request.getParameter("usecaseID");
            int usecaseID = Integer.parseInt(usecaseIDs);

             String srsIds = request.getParameter("srsId");
            int srsId = Integer.parseInt(srsIds);

             String errorDes = request.getParameter("errorDes");
             String passFailval = request.getParameter("passFailval");

            //   out.println("Response");
            boolean savingState = false;
           savingState = inspectorObj.SavingState(inspectionId,usecaseID,srsId,errorDes,passFailval);


if(savingState == true)
{
    out.println("Insertion/Updation is Sucessfull!");
}
else
{
out.println("Error in Saving/Updating, Please try again.");
}



        }
        if(inspectorAct.equals("newInspections"))
        {
             ArrayList<Inspection> tempInspection = new  ArrayList<Inspection>();
           tempInspection = inspectorObj.GetNewInspectionData(tempId);
//            out.println("Response from inspector controller");
           ss.setAttribute("NewInspectionObj", tempInspection);
        //building response

            out.println("<table width=\"80%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">");

            for(int i=0; i<tempInspection.size();i++)
            {
              //  String tempsrs = (String)tempInspection.get(i).SRSID;
               //loadData("+Integer.toString(tempInspection.get(i).SRSID)+","+Integer.toString(tempInspection.get(i).checkListID)+","+Integer.toString(tempInspection.get(i).inspectionID)+","+Integer.toString(tempInspection.get(i).inspectionNum)+","+tempInspection.get(i).inspectionDescription+","+tempInspection.get(i).inspectionName+")
                out.println("<tr>");
                 out.println("<td height=\"50\"><p  onclick=\"loadData("+tempInspection.get(i).SRSID+","+tempInspection.get(i).checkListID+","+tempInspection.get(i).inspectionID+","+tempInspection.get(i).inspectionNum+",'"+tempInspection.get(i).inspectionDescription+"','"+tempInspection.get(i).inspectionName+"')\"  class=\"smallred\">"+i+" "+tempInspection.get(i).inspectionName+"</p></td>");
                out.println("</tr>");
                out.println("<tr>");
                 out.println("<td class=\"date\">&nbsp;</td>");
                out.println("</tr>");
            }
               out.println("</table>");

           
        //building response


        }
        else if(inspectorAct.endsWith("saveSelectedKeyValuePair"))
        {


          //  usecaseIdd,selectedTextIs,srsIdd,InspectionIdd

            String tempUsecase = request.getParameter("usecaseid");
            String tempSRSID = request.getParameter("srsID");
            String tempInspectionId = request.getParameter("inspectionId");
            String selectedText = request.getParameter("selectedText");
            String textOfSnippet = request.getParameter("textOfSnippet");
        //    ArrayList<UseCases> tempInspection = new  ArrayList<UseCases>();
           int snippetSaving = inspectorObj.SaveErrosSnippets(tempSRSID,tempUsecase,tempInspectionId,selectedText,textOfSnippet);
 


            out.println("agyaaaaa" +tempInspectionId+tempSRSID+tempUsecase+selectedText+textOfSnippet);
        }
        else if(inspectorAct.equals("creatingmeetings"))
        {
        
        
        
           String tempUsecase = request.getParameter("usecaseid");
            String tempSRSID = request.getParameter("srsID");
            String tempInspectionId = request.getParameter("inspectionId");
         
                  int creatingMeetings = inspectorObj.createMeetings(tempSRSID,tempUsecase,tempInspectionId);
 
        
        }
        else if(inspectorAct.equals("getUsecases"))
        {

            String srsIds = request.getParameter("srsID");
            int srsId = Integer.parseInt(srsIds);
            //   out.println("Response");
           String checkList = request.getParameter("checklistID");
            int checkListidd = Integer.parseInt(checkList);
           
            ArrayList<UseCases> tempInspection = new  ArrayList<UseCases>();
           tempInspection = inspectorObj.GetUseCases(srsId);
 
           //building page
//------------------,
           ss.setAttribute("usecases1",tempInspection);

 ArrayList<CheckList> tempcheck = new  ArrayList<CheckList>();
           tempcheck = inspectorObj.GetCheckList(checkListidd);

        ss.setAttribute("checklist",tempcheck);


//S.put("checklist", tempcheck);
//out.println(S);

        // Getting check list in  this code.

           // String checkIds = request.getParameter("srsID");
            //int check Id = Integer.parseInt(srsIds);
            //   out.println("Response");
// ArrayList<CheckList> tempcheck = new  ArrayList<CheckList>();
  //         tempcheck = inspectorObj.GetCheckList(srsId);
           
     //   ss.setAttribute("checklist",tempcheck);

//S.put("checklist", tempcheck);
            
 /*
  ArrayList<CheckList> tempInspectionch = new  ArrayList<CheckList>();
           tempInspectionch = inspectorObj.GetCheckList(srsId);
 ss.setAttribute("checklist",tempInspectionch);
   */  //      out.println("yes");
           /*for(int i=0;i<tempInspection.size();i++)
{//This ID is use case ID, so that comments can be place according to correct usecase.
//out.println("<div class=\"slides_container\" style=\"margin-left: 220px; min-height: 700px; overflow-x: hidden; overflow-y: hidden; position: absolute; display: block; background-color: whitesmoke;color: slategrey;\">");
out.println("	<div style=\"position:absolute\">");
        	out.println("<h1 id=\"rtrtrt\">Usecase 1</h1>");
                out.println("    <b>Use Case Actors</b>");
           out.println("         <p>"+tempInspection.get(i).actors+"</p>");
                 out.println("   <b>Use case description</b>");
                out.println("    <p>"+tempInspection.get(i).useCaseDescription+"</p>");
                out.println("    <b>Use case preconditions </b>");
               out.println("     <p>"+tempInspection.get(i).preConditions+"</p>");
                out.println("    <b>Use Case Post conditions</b>");
                 out.println("   <p><"+tempInspection.get(i).postConditions+"</p>");

                out.println("    <b>Use Case Main Flow</b>");
                out.println("    <p>"+tempInspection.get(i).mainFlow+"</p>");
              out.println("      <b>Use Case Alternative Flow</b>");
               out.println("     <p>"+tempInspection.get(i).alternativeFlow+"</p>");


               out.println("    <div id=\"lowerAreaOfSlide\">");
             out.println("        <br>");
              out.println("       <br>");
             out.println("        <input type=\"text\"  placeholder=\"Add Description for this use Case\" style=\"background-color: white;height: 40px;width: 458px;\" />");
                out.println("     <br>");
                 out.println("    <br>");
                  out.println("   <input type=\"button\" id=\"completeusecase\" value=\"Complete\" style=\"background-color: wheat;height: 35px;width: 80px;\"/>");
                 out.println("    <p id=\"pause\" onclick=\"pauseplay()\">PAUSE</p>");
                out.println("     <input type=\"checkbox\"  style=\"margin-left: 45px;\" >Check to PASS this use case.</input>");
                  out.println("   <input type=\"checkbox\"  style=\"margin-left: 277px;\" >Done with this useCase</input>");
               out.println("     </div>");
   out.println("     </div>");

   
            }*/
        }

        else if(inspectorAct.equals("getcheckList"))
        {

            String srsIds = request.getParameter("srsID");
            int srsId = Integer.parseInt(srsIds);
            //   out.println("Response");
 ArrayList<CheckList> tempcheck = new  ArrayList<CheckList>();
           tempcheck = inspectorObj.GetCheckList(srsId);

       // ss.setAttribute("checklist",tempInspection);


//S.put("checklist", tempcheck);
//out.println(S);

        }
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet PreparationController</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet PreparationController at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */

 } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
